import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import * as API from "../api/http";

//存储数据
let initialState = {
    watchlist: [],//数据
}

const todosSlice = createSlice({
    name: "todos",
    initialState,
    reducers: { //同步操作

        //获取数据
        t_getwatchlist(state, action) {
            state.watchlist = action.payload
        },
    },
});

export const actionfn = todosSlice.actions;
export default todosSlice.reducer;

//异步操作 获取数据
export let async_getwatch = createAsyncThunk('async_getwatch', async (_, store) => {
    //调取接口
    let res = await API.http_getwatchlist()
    //同步修改异步
    store.dispatch(actionfn.t_getwatchlist(res))
})